<?php
App::uses('AppModel', 'Model');
/**
 * Article Model
 *
 * @property article_categorie $article_categorie
 * @property reponses_donnee $reponses_donnee
 * @property reponse $reponse
 */
class ActionQuestion extends AppModel {
	public function detruire_par_les_id($id_reponse, $id_question) {
		$this->query("DELETE FROM action_questions WHERE action_questions.id_reponse = ".$id_reponse." AND action_questions.id_question = ".$id_question);
	}
	
	public function removeByIdReponse($id_reponse) {
		$this->query("DELETE FROM action_questions WHERE action_questions.id_reponse = ".$id_reponse);
	}
	
	public function removeByIdQuestion($id_question) {
		$this->query("DELETE FROM action_questions WHERE action_questions.id_question = ".$id_question);
	}
	
	public function creer_ou_mettre_a_jour($id_question, $id_reponse, $active) {
		$result = $this->query("SELECT id_question, id_reponse, active_question FROM action_questions WHERE action_questions.id_question = ".$id_question." AND action_questions.id_reponse=".$id_reponse);

		if(!$result)
			$this->query("INSERT INTO action_questions (id_question, id_reponse, active_question) VALUES (".$id_question.", ".$id_reponse .",". ($active== 'active' ? 1 : 0).")");
		else 
			$this->query("UPDATE action_questions SET active_question=". ($active== 'active' ? 1 : 0)." where id_question =".$id_question." AND id_reponse=".$id_reponse );
	}
	
	public function recap_regles() {
		return $this->query("
			SELECT aq.id_question, r.id_reponse, q2.libelle_question, q2.type_reponse, a.description_article, r.libelle_reponse_combo, r.id_article, r.valeur_min, r.valeur_max, r.reponse_texte, r.reponse_oui_non, aq.active_question, q1.libelle_question, q1.type_reponse
			FROM action_questions aq 
			JOIN questions q2 on aq.id_question = q2.id_question
			JOIN reponses r on r.id_reponse = aq.id_reponse
			JOIN questions q1 on r.id_question = q1.id_question
			LEFT JOIN articles a on a.id_article = r.id_article");
	}
	
	public function recap_reponses() {
		return $this->query("
			SELECT * FROM nouveau_statuts ns 
			JOIN reponses r1 ON r1.id_reponse = ns.id_reponse
			JOIN questions q1 ON r1.id_question = q1.id_question
			JOIN reponses r2 ON r2.id_reponse = ns.id_reponse_statut_change_par	
			JOIN questions q2 ON q2.id_question = r2.id_question 
			JOIN statuts_reponses sr ON sr.id_statut_reponse = ns.id_statut_reponse
			LEFT JOIN articles a1 ON a1.id_article = r1.id_article
			LEFT JOIN articles a2 ON a2.id_article = r2.id_article
			WHERE sr.libelle_statut_reponse <> ''
			");
	}
	
/**
 * Primary key field
 *
 * @var string
 */
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'description_article';
/**
 * Validation rules
 *
 * @var array
 */
	//public $validate;

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
//	public $hasOne = array(
//		'question' => array(
//			'className' => 'articles_categorie',
//			'foreignKey' => 'id_categorie',
//			'conditions' => '',
//			'fields' => '',
//			'order' => ''
//		)
//	);

/**
 * hasMany associations
 *
 * @var array
 */
}
